<template>
    <div>
        <Form ref="formInline" :model="searchData" inline :label-width="70">
            <FormItem label="标题:">
                <Input type="text" v-model="searchData.realname" />
            </FormItem>
            <Button type="primary" @click="search()">搜索</Button>
            <Button @click="edit(false)" type="success" style="margin-left: 10px;">添加公告</Button>
        </Form>
        <Table :columns="columns" :data="list" @on-sort-change="customSort" :highlight-row="true" max-height="800" border
            stripe>
            <template slot-scope="{ row , index}" slot="typeSlot">
                <span v-if="row.type == 1">客户端</span>
                <span v-if="row.type == 2">技师端</span>
                <span v-if="row.type == 3">两端通用</span>
            </template>
            <template slot-scope="{ row , index}" slot="contextSlot">
                <div style="height: 200px;width: 100px;"><span v-html="row.context"></span></div>

            </template>
            <template slot-scope="{ row , index}" slot="makeSlot">
                <Tooltip content="编辑" style="margin-left: 10px;" placement="top">
                    <Icon type="ios-create" size="24" color="#2d8cf0" style="cursor: pointer;" @click="edit(row)" />
                </Tooltip>
                <Tooltip content="删除" style="margin-left: 10px;" placement="top">
                    <Icon type="md-trash" size="24" color="#ed4014" style="cursor: pointer;" @click.stop="del(row.id)" />
                </Tooltip>
            </template>
        </Table>
        <br>

        <Paginate :total="total" @on-change="changePage" @on-page-size-change="changeSize">
        </Paginate>


        <Drawer title="添加" :closable="false" width="80" v-model="showAdd">
            <Form ref="formValidate" :model="formItem" :label-width="100">
                <FormItem label="标题">
                    <Input v-model="formItem.title" placeholder=""></Input>
                </FormItem>
                <!-- <FormItem label="类型">
                    <radioGroup v-model="formItem.type">
                        <Radio label="1">客户端</Radio>
                        <Radio label="2">技师端</Radio>
                        <Radio label="3">两端通用</Radio>
                    </radioGroup>
                </FormItem> -->
                <!-- <FormItem label="缩略图" prop="goods_icon" class="item">
                    <Upload ref="upload" name="image" :action="action_url" :show-upload-list="false" :headers="header"
                        :format="['jpg', 'jpeg', 'png']" :on-format-error="handleFormatError" :on-success="handleSuccess">
                        <Button icon="ios-cloud-upload-outline">点击上传</Button>
                    </Upload>
                </FormItem>
                <FormItem class="item">
                    <img :src="baseImgeUrl + formItem.img" style="width: 200px;height: 200px;" v-show="formItem.img">
                </FormItem> -->
                <FormItem label="内容">
                    <div style="border: 1px solid #ccc;">
                        <Toolbar style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig"
                            :mode="mode" />
                        <Editor style="height: 500px; overflow-y: hidden;" v-model="formItem.content"
                            :defaultConfig="editorConfig" :mode="mode" @onCreated="onCreated" />
                    </div>
                </FormItem>
                <FormItem label="排序">
                    <Input v-model="formItem.sort" placeholder="数字越小越靠前" type="number"></Input>
                </FormItem>

                <FormItem>
                    <Button type="primary" @click="addQuestion()">提交</Button>
                </FormItem>
            </Form>
        </Drawer>
    </div>
</template>

<script>
import config from "@/config";
import { getToken } from "../../libs/util";
import Paginate from "@/components/paginate/paginate.vue";
const baseUrl = process.env.NODE_ENV === "development" ? config.baseUrl.dev : config.baseUrl.pro;
const baseImgeUrl = process.env.NODE_ENV === "development" ? config.baseImgUrl.dev : config.baseImgUrl.pro;
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'

export default {
    name: "articleList",
    components: {
        Editor, Toolbar, Paginate
    },
    data() {
        return {
            baseImgeUrl: baseImgeUrl,
            action_url: baseUrl + "/admin/image/uploadImg",
            header: { Authorization: getToken() },
            imgUrl: '',
            editor: null,
            html: '<p>hello</p>',
            toolbarConfig: {

            },
            editorConfig: {
                placeholder: '请输入内容...',
                MENU_CONF: {
                    uploadImage: {
                        server: baseUrl + "/admin/image/uploadImage",
                        fieldName: "image",
                        headers: {
                            Authorization: getToken()
                        },
                        // 单个文件上传成功之后
                        onSuccess(file, res) {
                        },
                    },
                    uploadVideo: {  //上传视频配置
                        server: baseUrl + "/admin/image/uploadImage",
                        fieldName: "image",
                        headers: {
                            Authorization: getToken()
                        }
                    },

                }
            },
            mode: 'default', // or 'simple',
            searchData: {
                status: 0,
                page: 1,
                limit: 10
            },
            // 列表
            columns: [
                {
                    title: '公告',
                    key: 'title',
                    sortable: 'custom',
                },
                // {
                //     title: '图片',
                //     key: 'img',
                //     render: (h, param) => {
                //         return h("img", {
                //             attrs: {
                //                 src: baseImgeUrl + param.row.img
                //             },
                //             style: {
                //                 width: '80px',
                //                 height: '60px'
                //             }
                //         });
                //     }
                // },
                // {
                //     title: '类型',
                //     key: 'type',
                //     slot: "typeSlot"
                // },
                {
                    title: '排序值',
                    key: 'sort',
                    sortable: 'custom',
                },
                {
                    title: '创建时间',
                    key: 'create_time',
                    sortable: 'custom',
                }, {
                    title: '操作',
                    slot: 'makeSlot',
                },
            ],
            // 数据
            list: [],
            total: 0,
            formItem: {
                id: 0,
                title: '',
                type: "1",
                content: '',
                img: "",
                sort: ""
            },
            showAdd: false,
        }
    },
    methods: {
        onCreated(editor) {
            this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
        },
        // 查询管理员信息
        getList() {
            let params = {
                ...this.searchData,
            };
            this.$axios('/admin/article/getList', params).then(res => {
                this.list = res.data;
                this.total = res.total;
            });
        },
        search() {
            this.searchData.page = 1;
            this.getList();
        },
        changePage(page) {
            this.searchData.page = page;
            this.getList();
        },
        changeSize(size) {
            this.searchData.limit = size;
            this.getList();
        },
        customSort(column, key, order) {
            this.searchData.order_by = column.key;
            this.searchData.sort = column.order;
            this.getList();
        },
        addQuestion() {
            this.$axios('/admin/article/save', this.formItem).then(res => {
                this.$Message.success('保存成功');
                this.formItem = {};
                this.showAdd = false;
                this.getList();
            });
        },
        edit(row) {
            if (row) {
                this.formItem.id = row.id;
                this.formItem.title = row.title + '';
                this.formItem.content = row.content;
                this.formItem.img = row.img;
                this.formItem.sort = row.sort;
                this.imgUrl = row.img;
                this.formItem.type = row.type + '';
            } else {
                this.formItem = {
                    id: 0,
                    title: '',
                    type: "1",
                    content: '',
                    img: "",
                    sort: ""
                }
            }
            this.showAdd = true;

        },
        del(id) {
            this.$axios('/admin/article/del', { id: id,status:1 }).then(res => {
                this.$Message.success('删除成功');
                this.getList();
            });
        },
        jump(id) {
            this.$router.push({ path: '/question?id=' + id });
        },
        handleSuccess(res, file) {
            this.formItem.img = res.data.src;
            this.imgUrl = res.data;
        },
        handleFormatError(file) {
            this.$Notice.warning({
                title: "文件格式错误",
                desc: "不支持该文件类型",
            });
        },

    },
    // mounted() {
    //     // 模拟 ajax 请求，异步渲染编辑器
    //     setTimeout(() => {
    //         this.html = '<p>模拟 Ajax 异步设置内容 HTML</p>'
    //     }, 1500)
    // },
    // beforeDestroy() {
    //     const editor = this.editor
    //     if (editor == null) return
    //     editor.destroy() // 组件销毁时，及时销毁编辑器
    // },
    created() {
        this.getList();
    }
}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
